package com.yanwq.pulltorefreshview.view;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.yanwq.pulltorefreshview.R;

/**
 * 作者：yanweiqiang。
 * 日期：2016/5/3 0003。
 */
public class PullToRefreshHeader extends FrameLayout implements PullToRefresh {
    private PullToRefreshProgressView pbp;
    private PullToRefreshListener listener;
    private Status status;

    private PullToActionUtil pullToActionUtil;

    @Override
    public void setListener(PullToRefreshListener listener) {
        this.listener = listener;
    }

    public PullToRefreshHeader(Context context) {
        super(context);
        init();
    }

    public PullToRefreshHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PullToRefreshHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        @SuppressLint("InflateParams") View rootView = LayoutInflater.from(getContext()).inflate(R.layout.pull_to_refresh_header, null);
        pbp = (PullToRefreshProgressView) rootView.findViewById(R.id.header_pbp);
        pullToActionUtil = new PullToActionUtil(rootView);
        addView(rootView);
    }

    @Override
    public Status getStatus() {
        return status;
    }

    @Override
    public void idle() {
        if (status == Status.IDLE) {
            return;
        }

        status = Status.IDLE;
        pullToActionUtil.animHeaderHeight(getHeight(), 0, null);
    }

    @Override
    public void pull(int distance) {

        if (status == Status.REFRESH || status == Status.FINISH) {
            return;
        }

        status = Status.PULL;
        pullToActionUtil.resizeHeaderHeight(distance);
        int angle = (int) ((float) distance / THRESHOLD * 360f);
        pbp.updateCircle(-90, angle > 360 ? 360 : angle);
    }

    @Override
    public void release(int distance) {

        if (status == Status.REFRESH || status == Status.FINISH) {
            return;
        }

        status = Status.RELEASE;

        if (distance >= THRESHOLD) {
            pullToActionUtil.animHeaderHeight(getHeight(), THRESHOLD, new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    refresh();
                    if (listener != null) {
                        listener.onRefresh();
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
        } else {
            idle();
        }
    }

    @Override
    public void refresh() {

        if (status == Status.REFRESH || status == Status.FINISH) {
            return;
        }

        status = Status.REFRESH;
        pbp.startProgress();
    }

    @Override
    public void finish() {

        if (status == Status.FINISH) {
            return;
        }

        status = Status.FINISH;
        pbp.stopProgress();

        postDelayed(new Runnable() {
            @Override
            public void run() {
                idle();
            }
        }, 1000);
    }

    @Override
    public void autoRefresh() {
        ValueAnimator animator = new ValueAnimator();
        animator.setIntValues(0, (int) (THRESHOLD + 10 * getResources().getDisplayMetrics().density));
        animator.setDuration(500);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int distance = (int) animation.getAnimatedValue();
                pull(distance);
            }
        });
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                release(getMeasuredHeight());
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
    }
}
